//
//  KFDPriceBoard.m
//  KFD
//
//  Created by JIRENTIANXIANG on 6/10/15.
//  Copyright (c) 2015 kfd. All rights reserved.
//

#import "KFDPriceBoard.h"
#import "ui_config.h"

@interface KFDPriceBoard ()

@property (strong,nonatomic) UILabel* typeLabel;
@property (strong,nonatomic) UILabel* priceLabel;
@property (strong,nonatomic) UILabel* growthLabel;
@property (strong,nonatomic) UIImageView* arrow;

@end

@implementation KFDPriceBoard

-(void)setPrice:(KFDMarketPrice *)price
{
    _price=price;
    self.typeLabel.text=price.name;
    self.priceLabel.text=[NSString stringWithFormat:@"%.2f",price.price.floatValue];
    self.growthLabel.text=[NSString stringWithFormat:@"%.2f%@",ABS(price.change_pro.floatValue),@"%"];
    if (price.change_pro.floatValue<0) {
        self.priceLabel.textColor=KFD_DOWN_COLOR;
        self.growthLabel.textColor=KFD_DOWN_COLOR;
        self.arrow.image=[UIImage imageNamed:@"down-ico"];
    }else{
        self.priceLabel.textColor=KFD_UP_COLOR;
        self.growthLabel.textColor=KFD_UP_COLOR;
        self.arrow.image=[UIImage imageNamed:@"up-ico"];
    }
}

-(instancetype)initWithFrame:(CGRect)frame
{
    CGSize size=frame.size;
    if (self=[super initWithFrame:frame]) {
        self.typeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, 30)];
        self.priceLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, size.width, 30)];
        self.growthLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        
        self.typeLabel.font=[UIFont systemFontOfSize:11];
        self.priceLabel.font=[UIFont systemFontOfSize:20];
        self.growthLabel.font=[UIFont systemFontOfSize:11];
        
        self.typeLabel.textAlignment=NSTextAlignmentCenter;
        self.priceLabel.textAlignment=NSTextAlignmentCenter;
        self.growthLabel.textAlignment=NSTextAlignmentCenter;
        
        self.typeLabel.textColor=KFDColorRGB(0x4d, 0x4d, 0x4d);
        
        [self addSubview:self.typeLabel];
        [self addSubview:self.priceLabel];
        [self addSubview:self.growthLabel];
        
        self.arrow=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        [self addSubview:self.arrow];
    }
    return self;
}

-(void)layoutSubviews
{
    CGSize size=self.bounds.size;
    self.typeLabel.center=CGPointMake(size.width*0.5, size.height*0.2);
    self.priceLabel.center=CGPointMake(size.width*0.5, size.height*0.5);
    self.growthLabel.center=CGPointMake(size.width*0.5+5, size.height*0.8);
    self.arrow.center=CGPointMake(self.growthLabel.frame.origin.x-5, self.growthLabel.center.y);
}

-(void)drawRect:(CGRect)rect
{
    CGPoint origin=rect.origin;
    CGSize size=rect.size;
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, KFDColorRGB(0xb3, 0xb3, 0xb3).CGColor);
    CGContextSetLineWidth(context, 1);
    CGContextMoveToPoint(context, origin.x, origin.y+size.height);
    CGContextAddLineToPoint(context, origin.x+size.width, origin.y+size.height);
    CGContextStrokePath(context);
    if (self.isSelected) {
        CGFloat xCenter=origin.x+size.width/2;
        CGFloat bottom=origin.y+size.height;
        CGPoint vertex0=CGPointMake(xCenter-6, bottom);
        CGPoint vertex1=CGPointMake(xCenter, bottom-4);
        CGPoint vertex2=CGPointMake(xCenter+6, bottom);
        CGPoint points[4]={
            vertex0,
            vertex1,
            vertex1,
            vertex2
        };
        CGContextStrokeLineSegments(context, points, 4);
        CGContextMoveToPoint(context, vertex0.x, vertex0.y);
        CGContextAddLineToPoint(context, vertex2.x, vertex2.y);
        CGContextSetStrokeColorWithColor(context, KFD_WHITE.CGColor);
        CGContextStrokePath(context);
        CGContextAddLines(context, points, 4);
        CGContextClosePath(context);
        CGContextSetFillColorWithColor(context, KFD_WHITE.CGColor);
        CGContextFillPath(context);
    }
}

-(void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    if (self.isSelected) {
        self.backgroundColor=KFDColorRGB(0xfb, 0xfb, 0xfb);
    }else{
        self.backgroundColor=KFD_WHITE;
    }
    [self setNeedsDisplay];
}

@end
